package com.corpgovernment.permission.controller;

import com.corpgovernment.api.permission.vo.*;
import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.base.JSONResult;
import com.corpgovernment.common.common.CorpBusinessException;
import com.corpgovernment.permission.service.UserService;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserRest {

    @Autowired
    private UserService userService;

    /**
     * uid获取所拥有的权限key
     */
    @PostMapping("/getUserPermissionList")
    public JSONResult<List<String>> getUserPermissionList(@RequestBody GetUserPermissionListRequest request) {
        log.info("请求参数：{}", request);
        List<String> userPermissionList = userService.getUserPermissionUrlList(request);
        return JSONResult.success(userPermissionList);
    }

    /**
     * uid获取所拥有的权限key
     */
    @PostMapping("/getUserPermissionListNew")
    public JSONResult<List<String>> getUserPermissionListNew(@RequestBody GetUserPermissionListRequest request) {
        List<String> userPermissionList = userService.getUserPermissionUrlList(request);
        return JSONResult.success(userPermissionList);
    }

    /**
     * uid获取所拥有的权限访问接口的Url
     */
    @PostMapping("/getUserPermissionUrlList")
    public JSONResult<List<String>> getUserPermissionUrlList(@RequestBody GetUserPermissionListRequest request) {
        List<String> userPermissionList = userService.getUserPermissionUrlList(request);
        return JSONResult.success(userPermissionList);

    }

    /**
     * uid获取所拥有的角色ID
     */
    @PostMapping("/getUserRolesList")
    public JSONResult<List<Long>> getUserRoleList(@RequestBody GetUserPermissionListRequest request) {
        List<Long> userPermissionList = userService.getUserRoleList(request);
        return JSONResult.success(userPermissionList);
    }


    @PostMapping("/getUserMenuList")
    public JSONResult<List<UserMenuTo>> getUserMenuList(BaseUserInfo baseUserInfo) {
        log.info("baseUserInfo:{}", JsonUtils.toJsonString(baseUserInfo));
        List<UserMenuTo> userMenuList = userService.getUserMenuListNew(baseUserInfo.getUid(), baseUserInfo.getBgUser());
        return JSONResult.success(userMenuList);
    }

    @PostMapping("/getUserMenuListSoa")
    public JSONResult<List<UserMenuTo>> getUserMenuListSoa(BaseUserInfo baseUserInfo) {
        List<UserMenuTo> userMenuList = userService.getUserMenuList(baseUserInfo.getUid(), baseUserInfo.getIsAdmin());
        return JSONResult.success(userMenuList);
    }

    @RequestMapping("/getRolesByUidAndOrgId")
    public JSONResult getRolesByUidAndOrgId(@RequestBody GetUserRoleDetailRequest req) {
        return JSONResult.success(userService.getUserRoleDetail(req));
    }

    /**
     * 获取角色帐号详情
     */
    @RequestMapping("/getUserRoleDetail")
    public JSONResult<GetUserRoleDetailResponse> getBkUserRoleDetail(@RequestBody GetUserRoleDetailRequest request) {
        return JSONResult.success(userService.getBkUserRoleDetail(request));
    }

    /**
     * 添加和修改后台帐号
     */
    @RequestMapping("/addOrUpdateBackgroundAccount")
    public JSONResult addOrUpdateBackgroundAccount(@RequestBody AddOrUpdateBackgroundAccountRequest request) {
        try {
            userService.addOrUpdateBackgroundAccount(request);
        } catch (CorpBusinessException e) {
            return JSONResult.errorMsg(e.getMsg());
        } catch (RuntimeException e) {
            log.error("addOrUpdateBackgroundAccount failed.{}", request, e);
            throw e;
        }
        return JSONResult.ok();
    }

    /**
     * 删除后台帐号
     */
    @RequestMapping("/deleteBackgroundAccount")
    public JSONResult deleteBackgroundAccount(@RequestBody DeleteBackgroundAccountRequest request) {
        userService.deleteBackgroundAccount(request.getStaffNumber());
        return JSONResult.ok();
    }
}
